package com.youkeda.app.control;

import com.youkeda.app.dataobject.UserDO;
import com.youkeda.app.model.Result;
import com.youkeda.app.model.User;
import com.youkeda.app.service.MatchDataService;
import com.youkeda.app.service.PersonalRecordService;
import com.youkeda.app.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName UserController
 * @Author 刘正星
 * @Date 2020/8/26 11:53
 **/
@RestController
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private MatchDataService matchDataService;
    @Autowired
    private PersonalRecordService personalRecordService;
    /**
     * 注册
     *
     * @return Result
     */
    @GetMapping("/registered")

    public Result<Boolean> registered(UserDO userDO) {
        //初始化数据填写自己的信息

        return userService.add(userDO);

    }
    /**
     * 登录
     *
     * @return Result
     */
    @GetMapping("/login")
    public Result<Boolean> login(@RequestParam("email") String email, @RequestParam("password") String password) {
        //初始化数据

       return userService.login(email,password);

    }
    @RequestMapping("/user/add")
    public Result<Integer> addUser(@RequestBody User user){
        Result<Integer> result =  new Result<>();
        result.setSuccess(true);
        int insert = userService.save(user);
        System.out.println("insert:  "+insert);
        if (insert <= 0){
            System.out.println(insert);
            result.setSuccess(false);
            result.setMessage("插入失败");
            result.setCode(String.valueOf(insert));
            return result;
        }
        result.setMessage("插入成功");
        return  result;
    }
    @RequestMapping("/delete")
    @ResponseBody
    public Result<Integer> deleteUser(Long userId){
        Result<Integer> result = new Result<>();
        result.setSuccess(true);
        User user = userService.get(userId);
        user.setValid(0);
        Integer update = userService.save(user);

        if (update ==null){
            result.setSuccess(false);
            return result;
        }
        result.setData(update);
        return result;
    }
}
